<?php
$config = new RM_Config();
$currencySymbol = $config->getValue('rm_config_currency_symbol');
$criteria = RM_Reservation_Manager::getInstance()->getCriteria();
$message = false;
$calculatedTotalPrice = 0;

if (isset($criteria) && ($criteria->start_datetime && $criteria->end_datetime)){

    if ($criteria !== null) {
        if ($criteria->start_datetime && $criteria->end_datetime) {
            $period = new RM_Reservation_Period(
                new RM_Date(strtotime($criteria->start_datetime)),
                new RM_Date(strtotime($criteria->end_datetime))
            );
        }
        
        $persons = new RM_Reservation_Persons(array("adults"=>$criteria->adults,"children"=>$criteria->children,"infants"=>$criteria->infants));

        $information = new RM_Prices_Information($this->unit, $period, $persons, $criteria->otherinfo);
        $priceSystem = RM_Environment::getInstance()->getPriceSystem();
     }

    try {
        $calculatedTotalPrice = $priceSystem->getTotalUnitPrice($information);
    } catch (Exception $e) {
        $calculatedTotalPrice = 0;
        $message = $e->getMessage();
    }

    if ((bool)$config->getValue('rm_config_prices_with_tax')) {
        $calculatedTotalPrice += RM_Environment::getInstance()->getTaxSystem()->calculateTotalTax($this->unit, $calculatedTotalPrice);
    }

    $pudoPriceModel = new RM_PickupDropoffPrices();
    if (isset($criteria) && ($criteria->pickup && $criteria->dropoff)){
        $pudoPrice = $pudoPriceModel->getPriceByLocationIDs($criteria->pickup, $criteria->dropoff);
        $pudoPrice = $pudoPrice['price'];
    } else {
        $pudoPrice = 0;
    }

    $pudoConfigModel = new RM_PickupDropoffConfig();
    $pudoConfig = $pudoConfigModel->get()->toArray();$pudoConfig = $pudoConfig[0];
    if ($pudoConfig['priceoption'] == 0){
        // include in price
        $calculatedTotalPrice += $pudoPrice;
    }

    /**
     * this checks if the unit has any partial reservations
     * This is only used when the flexible criteria option is selected.
     */
    if ($criteria->flexible && $message===false){
        $unitDAO = new RM_Units();
        $reservations = $unitDAO->isFlexibleAvailable($this->unit, $period);

        if (!empty($reservations)){
            $message = $this->_translate->_('User.Unit.Details','PartiallyAvailable');
        }
    }

}


//$dateFormat = $config->getJSDateformat();
$dateFormat = RM_Prices_Manager::getInstance()->getRealPriceSystem($this->unit)->getDateformat();

// panel config
$panelsettings = Zend_Json::decode($this->panel->settings);
$align = $panelsettings['align'];

?>
<div id="<?php echo $this->panel->getDivID() ?>">
    <div class="RM_unit_details_selectioninfo_container" style="text-align:<?php echo $panelsettings['align'];?>">
    <fieldset><legend><?php echo $this->_translate->_('User.Unit.Details','SelectionInfo');?></legend>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <?php
              if (isset($criteria) && ($criteria->start_datetime && $criteria->end_datetime)){
              ?>
             <tr>
                <td>
                    <?php echo $this->_translate->_('User.Unit.Details','SelectedStartDate');?>
                    &nbsp;
                    <b><?php echo date($dateFormat, strtotime($criteria->start_datetime));?></b>
                </td>
                <td rowspan="2">
                    <div align="center">
                        <a href="javascript:void(0)" onclick="rm_unit_details_selectDates(datepickerURL,datepickerWidth,datepickerHeight)">
                            <img src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/images/system/small/calendar_edit.png" border="0" alt="<?php echo $this->_translate->_('User.Unit.Details','ChangeSelection');?>" /><?php echo $this->_translate->_('User.Unit.Details','ChangeSelection');?>
                        </a>
                    </div>
                </td>
             </tr>
             <tr>
                <td>
                    <?php echo $this->_translate->_('User.Unit.Details','SelectedEndDate');?>
                    &nbsp;
                    <b><?php echo date($dateFormat, strtotime($criteria->end_datetime));?></b>
                </td>
             </tr>
              <?php
              } else {
              ?>
             <tr>
                <td colspan="2">
                    <div class="RM_unit_details_selectioninfo_warning">
                            <?php echo $this->_translate->_('User.Unit.Details','NoDatesSelected');?>
                    </div>
                </td>
             </tr>
             <?php
             }
             if ($calculatedTotalPrice>0){
             ?>
             <tr>
                <td colspan="2">
                    <?php echo $this->_translate->_('User.Unit.Details','SelectionTotal');?>
                    &nbsp;
                    <b><?php echo $currencySymbol; echo $calculatedTotalPrice;?></b>
                    <?php
                    if ($pudoConfig['priceoption'] == 1){
                        ?>
                        <br/>
                        <?php echo $this->_translate->_('User.PickupDropoff.Unit','PickupDropoffPrice');?>
                        <b><?php echo $currencySymbol; echo $pudoPrice;?></b>
                        <a href='javascript:void(0)' onclick="RM_doShadowBox('<?php echo RM_Environment::getInstance()->getRouter()->_('PickupDropoff', 'listlocations', array("unit_id"=>$this->unit->id, "page" => "listlocations.phtml"));?>', '', 700, 400, 'iframe' )" >
                        <img src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/images/system/small/edit.gif" border="0" alt="edit...">
                        </a>
                        <?php
                    }
                    ?>
                </td>
             </tr>
             <?php
             } else {
             ?>
             <tr>
                <td colspan="2">
                    <div class="RM_unit_details_selectioninfo_warning">
                        <?php echo $this->_translate->_('User.Unit.Details','TotalNotAvailable');?>
                    </div>
                </td>
             </tr>
             <?php
             }
             if (isset($message)){
             ?>
             <tr>
                <td colspan="2">
                    <div class="RM_unit_details_selectioninfo_warning">
                        <?php echo $message;?>
                    </div>
                </td>
             <?php
             }
             ?>
            </tr>
        </table>
    </fieldset>
    </div>
</div>